package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleCollectionMapper;
import com.heima.article.service.ApArticleCollectionService;
import com.heima.model.article.dtos.CollectionBehaviorDto;
import com.heima.model.article.pojos.ApCollection;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.APUserThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class ApArticleCollectionServiceImpl implements ApArticleCollectionService {
    @Autowired
    private ApArticleCollectionMapper apArticleCollectionMapper;
    @Override
    public ResponseResult collectionBehavior(CollectionBehaviorDto dto) {
        Integer userId = APUserThreadLocalUtil.getUser();
        ApCollection collection = new ApCollection();
        collection.setArticleId(dto.getEntryId());
        collection.setPublishedTime(dto.getPublishedTime());
        collection.setCollectionTime(new Date());
        collection.setType(dto.getType());
        collection.setEntryId(userId);
        //判断是否是收藏
        if (dto.getOperation() == 0){
            apArticleCollectionMapper.insert(collection);
        }else{
            LambdaQueryWrapper<ApCollection> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApCollection::getArticleId,dto.getEntryId());
            apArticleCollectionMapper.delete(queryWrapper);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
